package com.ncloud.resman.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.VirtualMachine;
import com.ncloud.resman.mapper.VirtualMachineMapper;
import com.ncloud.resman.service.SequenceService;
import com.ncloud.resman.service.VirtualMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by shiqiang on 2017/8/28.
 */
@Service
public class VirtualMachineServiceImpl implements VirtualMachineService{
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private VirtualMachineMapper virtualMachineMapper;
    @Override
    public int insertVirtualMachine(VirtualMachine virtualMachine) {
        if(virtualMachine.getVirtualId() == null){
           Long id = sequenceService.getSequence("virtual_id_seq");
            virtualMachine.setVirtualId(id);
        }
        if(virtualMachine.getCreateDate()==null){
            virtualMachine.setCreateDate(new Date());
        }
        return virtualMachineMapper.insertVirtualMachine(virtualMachine);
    }

    @Override
    public int updateVirtualMachine(VirtualMachine virtualMachine) {
        if(virtualMachine.getUpdateDate()==null){
            virtualMachine.setUpdateDate(new Date());
        }
        return virtualMachineMapper.updateVirtualMachine(virtualMachine);
    }

    @Override
    public VirtualMachine getVirtualMachineById(Long virtualId) {
        return virtualMachineMapper.getVirtualMachineById(virtualId);
    }

    @Override
    public List<VirtualMachine> getVirtualMachineByDeviceId(Long deviceId) {
        return virtualMachineMapper.getVirtualMachineByDeviceId(deviceId);
    }

    @Override
    public List<JSONObject> getRoomAll() {
        return virtualMachineMapper.getRoomAll();
    }

    @Override
    public List<JSONObject> getCabinetByRoomId(Long roomId) {
        return virtualMachineMapper.getCabinetByRoomId(roomId);
    }

    @Override
    public List<JSONObject> getDeviceServerByCabinetId(Long cabinetId) {
        return virtualMachineMapper.getDeviceServerByCabinetId(cabinetId);
    }

    @Override
    public int deleteVirtualMachine(Long virtualId) {
        return virtualMachineMapper.deleteVirtualMachine(virtualId);
    }

    @Override
    public JSONObject getDeviceServerBaseInfo(Long deviceId) {
        return virtualMachineMapper.getDeviceServerBaseInfo(deviceId);
    }

    @Override
    public int updateVirtualMachineState(Long virtualId,String state) {
        return virtualMachineMapper.updateVirtualMachineState(virtualId,state);
    }
}
